package com.cpi.cornpp.customer.converter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.customer.dto.CustomerDTO;
import com.cpi.cornpp.customer.dto.CustomerForecastDTO;
import com.cpi.cornpp.customer.exception.CustomerException;
import com.cpi.cornpp.customer.view.CustomerForecastView;
import com.cpi.cornpp.customer.view.CustomerView;
import com.cpi.cornpp.product.view.ProductView;
import com.cs.common.utils.StringUtils;

public class CustomerConverter {

	public final CustomerView convert(CustomerDTO customerDTO){
		CustomerView customerView = null;
		if (customerDTO != null){
			if( customerDTO.getCustomerId() !=null && !customerDTO.getCustomerId().trim().equals(StringUtils.EMPTY_STRING) ){
				customerView = new CustomerView();
				customerView.setCustomerId(customerDTO.getCustomerId().trim());			
				customerView.setDescription(customerDTO.getDescription().trim());
				customerView.setType(customerDTO.getType());
				customerView.setGroupId(customerDTO.getGroupId());
			}
		}
		return customerView;
	}
	
	public final List convertForListOfGroupView(int contracts, double cornContracted, List productViews, int purchaseInstructionDetailId){
		List customerForecastViews = null;
		CustomerForecastView customerForecastView = null;
		Iterator iterator = null;
		ProductView productView = null;
		iterator = productViews.iterator();
		customerForecastViews = new ArrayList(productViews.size());
		while( iterator.hasNext() ){
			productView = (ProductView)iterator.next();
			customerForecastView = new CustomerForecastView();
			customerForecastView.setContracts(contracts);
			customerForecastView.setCornContracted(cornContracted);
			customerForecastView.setItemId(productView.getProductId());
			customerForecastView.setPurchaseInstructionDetailId(purchaseInstructionDetailId);
			customerForecastViews.add(customerForecastView);
		}
		return customerForecastViews;
	}
	
	public final CustomerForecastDTO convertForListOfGroup(CustomerForecastView customerForecastView)throws CustomerException{
		CustomerForecastDTO customerForecastDTO = null;
		try{
			customerForecastDTO = new CustomerForecastDTO();
			customerForecastDTO.setContracts(customerForecastView.getContracts());
			customerForecastDTO.setCornContracted(customerForecastView.getCornContracted());
			customerForecastDTO.setItemId(customerForecastView.getItemId());
			customerForecastDTO.setPurchaseInstructionDetailId(customerForecastView.getPurchaseInstructionDetailId());
		}catch(Exception exception){
			CustomerException customerException=null;
			exception.printStackTrace(System.out);
			customerException= new CustomerException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw customerException;
		}
		return customerForecastDTO;
	}
	
	public final CustomerDTO convert(CustomerView customerView){
		CustomerDTO customerDTO = null;
		StringBuffer completeDescription = null;
		
		completeDescription = new StringBuffer(StringUtils.PERCENT_CHAR);
		if (customerView != null){
			customerDTO = new CustomerDTO();
			
			customerDTO.setCustomerId(customerView.getCustomerId());
			customerDTO.setDescription(completeDescription.append(customerView.getDescription()).append(StringUtils.PERCENT_CHAR).toString());

		}
		
		return customerDTO;
	}
	
	public final List convertViews(List customerViews){
		List customers = null;
		customers = new ArrayList(0);
		Iterator iterator = customerViews.iterator();
		CustomerView view = null;
		if (!CollectionUtils.isEmpty(customerViews)){
			while( iterator.hasNext() ){
				view = (CustomerView)iterator.next();
				customers.add(convert(view));
			}
		}
		
		return customers;
	}
	
	public final List convertDTOs(Collection customerDTO){
		List customers = null;
		customers = new ArrayList(0);
		Iterator iterator = customerDTO.iterator();
		CustomerDTO dto = null;
		CustomerView customerView = null;
		if (!CollectionUtils.isEmpty(customerDTO)){
			while(iterator.hasNext()){
				dto = (CustomerDTO)iterator.next();
				customerView = convert(dto);
				if( customerView !=null ){
					customers.add(customerView);
				}
			}
		}
		
		return customers;
	}
	
	public final List convertForecastDTOs(Collection customerForecastDTOs){
		List customerViews = null;
		customerViews = new ArrayList(0);
		Iterator iterator = customerForecastDTOs.iterator();
		CustomerForecastView customerForecastView = null;
		CustomerForecastDTO customerForecastDTO = null; 
		if (!CollectionUtils.isEmpty(customerForecastDTOs)){
			while(iterator.hasNext()){
				customerForecastDTO = (CustomerForecastDTO)iterator.next();
				customerForecastView = new CustomerForecastView();
				customerForecastView.setCustomerId(customerForecastDTO.getCustomerId());
				customerForecastView.setContracts(customerForecastDTO.getContracts());
				customerForecastView.setItemId(customerForecastDTO.getItemId());
				customerForecastView.setRemainingTons(customerForecastDTO.getRemainingTons());
				customerViews.add(customerForecastView);
			}
		}
		return customerViews;
	}
}
